const express = require('express')
const router = express.Router()
const db = require('../sql/db')

// 获取文章列表
router.get('/my/article/cates', function (req, res) {
    console.log(123);
    db(`select * from category`, function (err, data) {
        console.log(err);
        if (err || data[0].length == 0) return res.send({ status: 1, message: '获取文章列表失败' })
        res.send({ status: 0, message: '获取文章列表成功', data: data })
    })
})

// 新增文章分类
router.post('/my/article/addcates', function (req, res) {
    const { name, alias } = req.body
    console.log(res.body);
    db(`INSERT into category set alias='${alias}',name='${name}'`, function (err, data) {
        if (err || data.affectedRows == 0) return res.send({ status: 1, message: '新增分类失败' })
        res.send({ status: 0, message: '新增分类成功' })
    })
})
// 删除文章分类
router.get('/my/article/deletecate/:id', function (req, res) {
    const { id } = req.params
    db(`delete from category where id='${id}'`, function (err, data) {
        if (err || data.affectedRows == 0) return res.send({ status: 1, message: '删除文章分类失败' })
        res.send({ status: 0, message: '删除文章分类成功' })
    })
})
// 根据id获w取文章
router.get('/my/article/cates/:id', function (req, res) {
    const { id } = req.params
    db(`select * from category where id='${id}'`, function (err, data) {
        if (err || data.affectedRows == 0) return res.send({ status: 1, message: '获取文章分类失败' })
        res.send({ data })
    })
})



// 根据 Id 更新文章分类数据
router.post('/my/article/updatecate', function (req, res) {
    const { id, name, alias } = req.body
    db(`update category set name='${name}', alias='${alias}' where id=${id}`, function (err, data) {
        console.log(data);
        if (err) return res.send({ status: 1, message: '更改文章分类失败' })
        res.send({
            "status": 0,
            "message": "更新分类信息成功！"
        })
    })
})

module.exports = router